﻿using System;
//using System.Web.UI.MobileControls;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;

/// <summary>
/// Summary description for CE
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class ceTerm : System.Web.Services.WebService
{
    static ConfExpert.Utility.Data.DataManager dm;

    public class MyXmlTextWriter : System.Xml.XmlTextWriter
    {
        public MyXmlTextWriter(System.IO.TextWriter w)
            : base(w)
        {
            //Formatting = System.Xml.Formatting.Indented;
        }

        public override void WriteStartDocument()
        {
            //to suppress
        }
    }

    public static string Serialize(DataTable table)
    {
        //EndEdits();
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add(String.Empty, String.Empty);
        StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
        XmlTextWriter tw = new MyXmlTextWriter(sw);
        DataSet ds = new DataSet("MyData");
        ds.Tables.Add(table);
        ds.WriteXml(tw, XmlWriteMode.IgnoreSchema);
        return sw.ToString();
    }

    public ceTerm()
    {
        SetDB();
    }

    public static void SetDB()
    {
        string cs = string.Empty;
        cs = @"Data Source=10.0.0.4;Initial Catalog=ConfExpert;Persist Security Info=True;Integrated Security=False;User ID=ConfExpertUser;Password=10341518";
        cs = @"Data Source=confexpert.karbel.com;Initial Catalog=ConfExpert;Persist Security Info=True;Integrated Security=False;User ID=ConfExpertUser;Password=10341518";

        dm = new ConfExpert.Utility.Data.DataManager(cs);
    }

    [WebMethod]
    public DataSet GetTopByGelisPartisi(int GelisPartisiId)
    {
        return dm.ExecuteDs("GetTopByGelisPartisi", GelisPartisiId);
    }

    //[WebMethod]
    //public DataTable GetLastGelisPartisi()
    //{
    //    //DbCommand cmd = dm.GetCommand("GetLastGelisPartisi", CommandType.StoredProcedure);
    //    //cmd.Parameters.Add(new SqlParameter("@count", 10));
    //    return dm.ExecuteDt("GetLastGelisPartisi", 10);
    //}

    //[WebMethod]
    //public List<int> GetRollId()
    //{
    //    //DbCommand cmd = dm.GetCommand("GetRollId", CommandType.StoredProcedure);

    //    DataTable dt = dm.ExecuteDt("GetRollId");
    //    List<int> rollIds = new List<int>();

    //    for (int i = 0; i < dt.Rows.Count; i++)
    //        rollIds.Add((int)dt.Rows[i]["RollId"]);

    //    return rollIds;
    //}

    //[WebMethod]
    //public List<int> GetSayimEksigi()
    //{
    //    //DbCommand cmd = dm.GetCommand("GetSayimEksigi", CommandType.StoredProcedure);

    //    DataTable dt = dm.ExecuteDt("GetSayimEksigi");
    //    List<int> rollIds = new List<int>();

    //    for (int i = 0; i < dt.Rows.Count; i++)
    //        rollIds.Add((int)dt.Rows[i]["RollId"]);

    //    return rollIds;
    //}

    [WebMethod]
    public List<int> GetRafiOlmayanToplar()
    {
        DataTable dt = dm.ExecuteDt("GetRafiOlmayanToplar");
        List<int> rollIds = new List<int>();

        for (int i = 0; i < dt.Rows.Count; i++)
            rollIds.Add((int)dt.Rows[i]["RollId"]);

        return rollIds;
    }


    [WebMethod]
    public DataSet GetTopByCikisEmri(int CikisEmriId)
    {
        return dm.ExecuteDs("GetTopByCikisEmri", CikisEmriId);
    }

    [WebMethod]
    public DataSet GetNewRollIds(string oldRollIds)
    {
        return dm.ExecuteDs(prefix + "GetNewRollIds", oldRollIds);
    }

    [WebMethod]
    public void OPUpdRfid(int newRollId)
    {
        dm.ExecuteNq(prefix + "OPUpdRfid", newRollId);
    }

    string prefix = "term";
    [WebMethod]
    public string OPGetRollDetail(int RollId)
    {
        DataTable dt = dm.ExecuteDt(prefix + "OPGetRollDetail", RollId);
        if (dt.Rows.Count > 0)
            return dt.Rows[0][0].ToString();
        else
            return "";
    }

    [WebMethod]
    public void UpdRoll(int RollId,decimal Qty)
    {
        dm.ExecuteNq(prefix + "UpdRoll", RollId,Qty);      
    }


    [WebMethod]
    public DataSet GetTopByRollId(string rolls)
    {
        return dm.ExecuteDs("GetTopByRollId", rolls);
    }

    //[WebMethod]
    //public int TopEtiketle(int RollId)
    //{
    //    //DbCommand cmd = dm.GetCommand("TopEtiketle", CommandType.StoredProcedure);
    //    //cmd.Parameters.Add(new SqlParameter("@RollId", RollId));
    //    return Convert.ToInt32(dm.ExecuteSc("TopEtiketle", RollId));
    //}

    [WebMethod]
    public int TopEtiketle2(int RollId, int ShelfId)
    {        
        return Convert.ToInt32(dm.ExecuteSc("TopEtiketle2", RollId, ShelfId));
    }

    [WebMethod]
    public void TopYerlestir(DataTable dtYerlesenToplar)
    {     
        dm.ExecuteSc("TopYerlestir", Serialize(dtYerlesenToplar));
    }

    [WebMethod]
    public string Test()
    {
        return "test";
    }

    //[WebMethod]
    //public DataSet GetRollsToCut(int CikisEmriId)
    //{
    //    //DbCommand cmd = dm.GetCommand("GetRollCut_WS", CommandType.StoredProcedure);
    //    //cmd.Parameters.Add(new SqlParameter("@CikisEmriId", CikisEmriId));
    //    return dm.ExecuteDs("GetRollCut_WS", CikisEmriId);
    //}

    [WebMethod]
    public DataSet GetEtiketlenmeyenToplar()
    {
        return dm.ExecuteDs("GetEtiketlenmeyenToplar");
    }

    [WebMethod]
    public DataTable GetCikisEmirleri()
    {
        return dm.ExecuteDt("GetCikisEmirleri");
    }

    [WebMethod]
    public DataTable GetRafDetay(int ShelfId)
    {     
        return dm.ExecuteDt("GetRafDetay", ShelfId);
    }

    [WebMethod]
    public DataSet GetTopDetay(int RollId, int Photo)
    {     
        return dm.ExecuteDs("GetTopDetay", RollId, Photo);
    }

    public byte[] GetTinyEyeProductPhoto(int ProductId)
    {
       return  (byte[]) dm.ExecuteDt("GetTinyEyeProductPhoto", ProductId).Rows[0][0];
    }

    [WebMethod]
    public DataTable Ayarlar()
    {     
        return dm.ExecuteDt("GetTermSettings");
    }

    [WebMethod]
    public bool ParolaDogrula(string parola)
    {
        string yoneticiParola = (string)Ayarlar().Select("Name = 'TermYoneticiParola'")[0]["Val"];
        return parola == yoneticiParola;
    }

    [WebMethod]
    public int InsBuffer(int TerminalId, DataTable val, char BufferType)
    {
        return Convert.ToInt32(dm.ExecuteDt("InsBuffer", TerminalId, Serialize(val), BufferType).Rows[0]["BufferId"]);
    }
    
    [WebMethod]
    public int InsOPRolls(int TerminalId, string commaSeperatedRollIds)
    {
        DataTable dt = dm.ExecuteDt(prefix + "InsOPRolls", TerminalId, commaSeperatedRollIds);
        return Convert.ToInt32(dt.Rows[0][0]);
    }

    public DataTable GetBuffer(int BufferTempId)
    {
        DataSet ds = new DataSet();
        StringReader sr = new StringReader(dm.ExecuteDt("GetBufferTemp", BufferTempId).Rows[0]["val"].ToString());
        ds.ReadXml(sr);
        return ds.Tables[0];
    }

    //[WebMethod]
    //public int InsSayim(DataTable dt, int Terminal, int OncekiEksik)
    //{
    //    //DbCommand cmd = dm.GetCommand("InsSayim", CommandType.StoredProcedure);
    //    //cmd.Parameters.Add(new SqlParameter("@xml", Serialize(dt)));
    //    //cmd.Parameters.Add(new SqlParameter("@Terminal", Terminal));
    //    //cmd.Parameters.Add(new SqlParameter("@OncekiEksik", OncekiEksik));
    //    //return dm.ExecuteNonQuery(cmd);

    //    return Convert.ToInt32(dm.ExecuteSc("InsSayim", Serialize(dt), Terminal, OncekiEksik));
    //}

    [WebMethod]
    public DataTable GetRacks()
    {        
        return dm.ExecuteDt("GetRacks");
    }
}